package com.creative.accountsdk;

import android.content.Context;
import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.creative.accountsdk.holder.Constants;
import com.creative.util.DataUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by yinjialiang on 15-6-17.
 */
public class UserData {
    public final static int ACCOUNT_TYPE_GUEST = 0;
    public final static int ACCOUNT_TYPE_NORMAL = 1;
    public final static int ACCOUNT_TYPE_WEIXIN = 2;
    public final static int ACCOUNT_TYPE_QQ = 3;
    public final static int ACCOUNT_TYPE_WEIBO = 4;
    public final static int ACCOUNT_TYPE_FACEBOOK = 5;

    private long uid;
    private String token;
    private String ust;
    private long registeTime;
    private String nickname;

    private JSONObject json;
    private UserInfor  userinfor ;

    private final static String KEY_USER_DATA = "account.data.UserData";

    private UserInfor   parentinfo;
    private final static String KEY_USER_PARENT_DATA = "account.data.parent.UserData";

    private ArrayList<UserInfor> kidinfo;
    private final static String KEY_USER_KIDS_DATA = "account.data.kids.UserData";

    private ClassInfo    classInfo;
    private final static String KEY_USER_ClASS_DATA = "account.data.ClassInfo";


    private final static String TAG = "UserData";

    private static UserData instance = null;

    public static UserData getInstance() {
        if(instance == null)
        {
            instance = new UserData();
            instance.read(AccountSdk.getApplication());
        }
        return instance;
    }

    public static void setInstance(UserData instance) {
        UserData.instance = instance;
    }

    private UserData()
    {}

    public void read(Context context)
    {
        String jsontext = DataUtil.readString(context, KEY_USER_DATA);
        if(jsontext != null)
        {
            Log.d(TAG, "json = " + jsontext);
            try {
                init(new JSONObject(jsontext));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if (getRole() == 2) {  //如果是孩子，获取父母信息
            String jsontextParent = DataUtil.readString(context, KEY_USER_PARENT_DATA);
            if (jsontextParent != null) {
                Log.d(TAG, "jsontextParent = " + jsontextParent);
                try {
                    initParent(new JSONObject(jsontextParent));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            String jsontextClassInfo = DataUtil.readString(context, KEY_USER_ClASS_DATA);
            if (jsontextClassInfo != null) {
                Log.d(TAG, "jsontextClassInfo = " + jsontextClassInfo);
                try {
                    initClassInfo(new JSONObject(jsontextClassInfo));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        } else if (getRole() == 1) {  //如果是家长，获取孩子信息
            String jsontextKids = DataUtil.readString(context, KEY_USER_KIDS_DATA);
            if (jsontextKids != null) {
                Log.d(TAG, "jsontextKids = " + jsontextKids);
                try {
                    initKids(new JSONArray(jsontextKids));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }



    }

    public void write(Context context)
    {
        if(json != null) {
            Log.d(TAG, "write");
                 DataUtil.writeString(context, KEY_USER_DATA, json.toString());
            if (json.has("parent")) {
                DataUtil.writeString(context, KEY_USER_PARENT_DATA, json.optJSONObject("parent").toString());
            }
            if (json.has("class")) {
                DataUtil.writeString(context, KEY_USER_ClASS_DATA, json.optJSONObject("class").toString());
            }
        }
    }

    public void writeUserInfo(Context context, JSONObject jsonUser)
    {
        if(jsonUser != null) {
            Log.d(TAG, "writeUserInfo");
            DataUtil.writeString(context, KEY_USER_DATA, jsonUser.toString());
        }
    }


    public void writeParent(Context context, JSONObject parentJson)
    {
        Log.d(TAG, "writeParent");
        if(parentJson != null) {
            DataUtil.writeString(context, KEY_USER_PARENT_DATA, parentJson.toString());
        } else {
            DataUtil.writeString(context, KEY_USER_PARENT_DATA, null);
        }
    }

    public void writeClassInfo(Context context, JSONObject classJson)
    {
        Log.d(TAG, "writeClassInfo");
        if(classJson != null) {
            DataUtil.writeString(context, KEY_USER_ClASS_DATA, classJson.toString());
        } else {
            DataUtil.writeString(context, KEY_USER_ClASS_DATA, null);
        }
    }

    public void writeKids(Context context, JSONArray kidsJsonArray)
    {
        Log.d(TAG, "writeKids");
        if(kidsJsonArray != null) {
            DataUtil.writeString(context, KEY_USER_KIDS_DATA, kidsJsonArray.toString());
        } else {
            DataUtil.writeString(context, KEY_USER_KIDS_DATA, null);
        }
    }


    public JSONObject getJson()
    {
        return json;
    }

    public boolean isGuest()
    {
        if(isBindWawayaya() || isBindQQ() || isBindWeibo() || isBindWeixin())
        {
            return false;
        }
        return true;
    }

    public void init(JSONObject json)
    {
        Log.d(TAG, "init json = " + json);
        this.json = json;
        this.uid = json.optLong("uid");
        this.token = json.optString("token");
        if(json.has("ust")) {
            this.ust = json.optString("ust");
        }
        if(json.has("regtime"))
        {
            registeTime = json.optLong("regtime");
        }
        if(json.has("uinfo")){
            userinfor = new UserInfor().setJsonToVo(json.optJSONObject("uinfo"));
        }
        if(json.has("parent")) {
            parentinfo = new UserInfor().setJsonToVo(json.optJSONObject("parent"));
        }
        if(json.has("class")) {
            classInfo = new ClassInfo().setJsonToVo(json.optJSONObject("class"));
        }
        nickname = json.optString("nickname");
        debug();
    }

    public void initUserInfo(JSONObject jsonUser) {
        if (jsonUser != null) {
            userinfor = new UserInfor().setJsonToVo(jsonUser);
        } else {
            userinfor = null;
        }
    }


    public void initParent(JSONObject json)
    {
        if (json != null) {
            parentinfo = new UserInfor().setJsonToVo(json);
        } else {
            parentinfo = null;
        }
    }

    public void initClassInfo(JSONObject json)
    {
        if (json != null) {
            classInfo = new ClassInfo().setJsonToVo(json);
        } else {
            classInfo = null;
        }
    }

    public void initKids(JSONArray jsonArray)
    {
        if (kidinfo != null) {
            kidinfo.clear();
            kidinfo = null;
        }

        if (jsonArray != null && jsonArray.length() > 0) {
            kidinfo = new ArrayList<UserInfor>();
            for (int i = 0; i < jsonArray.length(); i++) {
                UserInfor kid = new UserInfor().setJsonToVo(jsonArray.optJSONObject(i));
                kidinfo.add(kid);
            }
        }
    }



    public void clear(Context context)
    {
        DataUtil.remove(context, KEY_USER_DATA);
        DataUtil.remove(context,KEY_USER_PARENT_DATA);
        DataUtil.remove(context, KEY_USER_KIDS_DATA);
    }

    public boolean exist()
    {
        Log.d(TAG, "exit中的数据 uid = " + uid + ", token = " + token);
        return uid > 0L && token != null;
    }

    public void debug()
    {
        Log.d(TAG, "uid = " + uid + ", token = " + token);
        Log.d(TAG, "json = " + json);
    }

    public boolean isBindWawayaya()
    {
        return ust.charAt(1) == '1';
    }

    public boolean isBindWeibo()
    {
        return ust.charAt(4) == '1';
    }

    public boolean isBindQQ()
    {
        return ust.charAt(3) == '1';
    }

    public boolean isBindWeixin()
    {
        return ust.charAt(2) == '1';
    }

    public boolean isBindFacebook()
    {
        return ust.charAt(5) == '1';
    }

    public String getLoginName(int platform)
    {
        switch (platform)
        {
            case UserData.ACCOUNT_TYPE_NORMAL:
                if(isBindWawayaya())
                {
                    return json.optString("loginname");
                }
                break;
            case UserData.ACCOUNT_TYPE_QQ:
                if(isBindQQ())
                {
                    return json.optString("qqnickname");
                }
                break;
            case UserData.ACCOUNT_TYPE_WEIBO:
                if(isBindWeibo())
                {
                    return json.optString("wbnickname");
                }
                break;
            case UserData.ACCOUNT_TYPE_WEIXIN:
                if(isBindWeixin())
                {
                    return json.optString("wxnickname");
                }
                break;
            case UserData.ACCOUNT_TYPE_FACEBOOK:
                if(isBindFacebook())
                {
                    return json.optString("fbnickname");
                }
                break;
        }
        return AccountSdk.getApplication().getResources().getString(R.string.not_bind);
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
        try {
            json.put("nickname", nickname);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public long getUid() {
        return uid;
    }

    public String getToken() {
        return token;
    }

    public long getRegisteTime() {
        return registeTime;
    }

    public UserInfor  getUserinfor() {
        return userinfor;
    }

    public UserInfor  getParentinfor() {
        return parentinfo;
    }

    public ClassInfo  getClassinfor() {
        return classInfo;
    }


    public int getRole() {
        if (userinfor != null) {
            return userinfor.role;
        }
        return 0;
    }

    public void  save(JSONObject  json,Context context){
        UserData.getInstance().init(json);
        UserData.getInstance().write(context);
    }
}
